package com.hwz.gulimall.product.thread;

import lombok.Data;
import org.springframework.boot.task.TaskExecutorCustomizer;

import java.util.concurrent.*;
import java.util.function.Supplier;

/**
 * @Author: hwz
 * @CreateTime: 2025-09-14
 */


public class ThreadTest {
    public static void main(String[] args) {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                2,
                5,
                100,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(5),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy()
        );
        try {
            CompletableFuture<Integer> f1 = CompletableFuture.supplyAsync(() -> {
                System.out.println("当前线程：" + Thread.currentThread().getId());
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                return 1;
            }, executor);
            CompletableFuture<Integer> f2 = CompletableFuture.supplyAsync(() -> {
                System.out.println("当前线程：" + Thread.currentThread().getId());
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                return 2;
            }, executor);
            CompletableFuture<Integer> f3 = CompletableFuture.supplyAsync(() -> {
                System.out.println("当前线程：" + Thread.currentThread().getId());
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                return 3;
            }, executor);


//            CompletableFuture<Void> future = f1.runAfterBoth(f2, () -> {}).runAfterBoth(f3, ())



//            System.out.println( i);
            System.out.println("主线程继续执行...");

        }catch (Exception e){

        }


    }
}
@Data
class Task implements Runnable{
    private int i;
    public Task(int i) {
        this.i = i;
    }
    @Override
    public void run() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("当前线程：" + Thread.currentThread().getId() + "正在执行任务" + i);
    }
}
